// store/user.ts
import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => ({
    token: localStorage.getItem('token') || '',
    userInfo: JSON.parse(localStorage.getItem('userInfo') || '{}'),
    permission: JSON.parse(localStorage.getItem('permission') || '[]'),
  }),
  actions: {
    setToken(token: string) {
      this.token = token
      localStorage.setItem('token', token)
    },
    setUserInfo(info: Record<string, unknown>) {
      this.userInfo = info
      localStorage.setItem('userInfo', JSON.stringify(info))
    },
    setPermission(permission: string[]) {
      this.permission = permission
      localStorage.setItem('permission', JSON.stringify(permission))
    },
    logout() {
      this.token = ''
      this.userInfo = {}
      this.permission = []
      localStorage.removeItem('token')
      localStorage.removeItem('userInfo')
      localStorage.removeItem('permission')
    }
  }
})
